package com.hangxiang.approval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hangxiang.approval.common.GroupManagerCommon;
import com.hangxiang.approval.common.GroupManagerInsertCommon;
import com.hangxiang.approval.common.GroupManagerUpdateCommon;
import com.hangxiang.approval.common.IntegerCommon;
import com.hangxiang.approval.dao.GroupManagerDao;
import com.hangxiang.approval.dao.UserDao;
import com.hangxiang.approval.dao.UserManagerDao;
import com.hangxiang.approval.entity.GroupManager;
import com.hangxiang.approval.entity.User;
import com.hangxiang.approval.entity.UserManager;
import com.hangxiang.approval.service.GroupManagerService;
import com.hangxiang.approval.utils.BeanConvertUtils;
import com.hangxiang.approval.vo.GroupManagerVo;
import com.hangxiang.approval.vo.PeopleVo;
import com.hangxiang.approval.vo.UserManagerVo;
import com.hangxiang.approval.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (Groupmanager)表服务实现类
 *
 * @author makejava
 * @since 2020-11-23 18:06:30
 */
@Service("groupmanagerService")
public class GroupManagerServiceImpl extends ServiceImpl<GroupManagerDao, GroupManager> implements GroupManagerService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private GroupManagerDao groupmanagerDao;
    @Resource
    private UserManagerDao userManagerDao;
    @Resource
    private UserDao userDao;
    /**
     * 通过ID查询单条数据
     *
     * @param common 主键
     * @return 实例对象
     */
    @Override
    public GroupManagerVo queryById(IntegerCommon common) {
        GroupManagerVo vo = new GroupManagerVo();
        GroupManager groupmanager = groupmanagerDao.selectById(common.getValue());
        BeanUtils.copyProperties(groupmanager, vo);
        return vo;
    }


    /**
     * 新增数据
     *
     * @param groupmanagerInsertCommon 实例对象
     * @return 实例对象
     */
    @Override
    public Integer insertGroupManager(GroupManagerInsertCommon groupmanagerInsertCommon) {
        GroupManager groupmanager = new GroupManager();
        BeanUtils.copyProperties(groupmanagerInsertCommon, groupmanager);
        groupmanagerDao.insert(groupmanager);
        return groupmanager.getId();
    }

    /**
     * 修改数据
     *
     * @param groupmanagerUpdateCommon 实例对象
     * @return 实例对象
     */
    @Override
    public Integer updateGroupmanager(GroupManagerUpdateCommon groupmanagerUpdateCommon) {
        GroupManager groupmanager = new GroupManager();
        BeanUtils.copyProperties(groupmanagerUpdateCommon, groupmanager);
        groupmanagerDao.updateById(groupmanager);
        return groupmanager.getId();
    }



//    @Override
//    public List<GroupManagerVo> selectAllInfo() {
//        List<GroupManagerVo> managerList=groupmanagerDao.selectAllInfo();
//        List<GroupManagerVo> groupManagerVoList=new ArrayList<>();
//        GroupManagerVo managerVo=new GroupManagerVo();
//        managerList.forEach(groupManager -> {
//            List<UserVo> userVos=userManagerDao.getAllUserInfo(groupManager.getId());
//            managerVo.setDepartmentName(groupManager.getDepartmentName());
//            managerVo.setId(groupManager.getId());
//        });
//        groupManagerVoList.add(managerVo);
//        return groupManagerVoList;
//    }

    @Override
    public GroupManagerVo selectInfoByUserId(GroupManagerCommon common) {
        GroupManagerVo groupManagerVo=groupmanagerDao.selectByUserId(common.getId());
        return groupManagerVo;
    }

    @Override
    public List<GroupManagerVo> selectDepartment() {
        List<GroupManager> managers=groupmanagerDao.selectList(new QueryWrapper<GroupManager>().lambda().eq(GroupManager::getIsDeleted,0));
        List<GroupManagerVo> groupManagerVoList=BeanConvertUtils.convertListTo(managers,GroupManagerVo::new);
        return groupManagerVoList;
    }

    @Override
    public List<UserManagerVo>  selectDepartmentUser(IntegerCommon common) {
        List<UserManager> userManagers=userManagerDao.selectList(new QueryWrapper<UserManager>().lambda().eq(UserManager::getGroupId,common.getValue()));
        List<UserManagerVo> userManagerVos= BeanConvertUtils.convertListTo(userManagers, UserManagerVo::new);
        userManagerVos.forEach(userManagerVo -> {
            User user=userDao.selectOne(new QueryWrapper<User>().lambda().eq(User::getId,userManagerVo.getUserId()));
            userManagerVo.setPhone(user.getPhone());
        });
        return userManagerVos;
    }


}